package com.zhao.arithmetic;

import java.util.Arrays;

/**
 * Description: <br/>
 * Copyright (c) ，2020 ， 赵 <br/>
 * This program is protected by copyright laws. <br/>
 * Date： 2020/12/30 15:30
 *
 * @author 柒柒
 * @version : 1.0
 * @Update [序号][日期YYYY-MM-DD] [更改人姓名][变更描述]
 */

public class BubleSort {
    public static void main(String[] args) {
        int[] array =new int[10];
        for (int i = 0; i < array.length; i++) {
            array[i] = (int)(Math.random()*100);
        }
        System.out.println("排序之前:"+Arrays.toString(array));
        bubbleSort(array);
        System.out.println("排序之后:"+Arrays.toString(array));
    }

    public static void bubbleSort(int[] data){
        System.out.println("开始排序:");
        int arrayLength = data.length;
        //控制循环的次数
        int i;
        for (i = 0; i < arrayLength; i++) {
            //有序标记,每一列的初始值为true
            boolean flag = true;
            for (int j = 0; j < arrayLength-i-1; j++) {
                //之前位数已经排序过,不用再排,最后一个不用再排
                if (data[j] > data[j+1]){
                    int tmp = data[j];
                    data[j] = data[j+1];
                    data[j+1] = tmp;
                    flag = false;
                }
            }
            System.out.println("第"+(i+1)+"次排序:"+Arrays.toString(data));
            if (flag){
                break;
            }
        }
    }
}

















